package com.meitianhui.convenitenbuy.main.http;

import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.http.RetrofitHelper;
import com.meitianhui.convenitenbuy.main.model.bean.AppToken;
import com.meitianhui.convenitenbuy.main.model.bean.CollectedStores;
import com.meitianhui.convenitenbuy.main.model.bean.HyphenateInfo;
import com.meitianhui.convenitenbuy.main.model.bean.PhoneCodeInfo;
import com.meitianhui.convenitenbuy.main.model.bean.StringResponse;
import com.meitianhui.convenitenbuy.main.model.bean.UpdateBean;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.main.model.bean.VerifyPhone;
import com.meitianhui.convenitenbuy.main.model.bean.VersionInfo;
import com.meitianhui.convenitenbuy.module.mine.model.ConsumerInfo;
import com.meitianhui.convenitenbuy.module.mine.model.UserAsset;
import com.meitianhui.convenitenbuy.module.mine.model.UserInfo;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.wallet.model.RedPacketBean;
import com.meitianhui.convenitenbuy.util.RxUtils;

import okhttp3.ResponseBody;
import retrofit2.http.Query;
import retrofit2.http.Url;
import rx.Observable;

/**
 *
 */

public class MainApiDataSource implements MainApi {
    private static volatile MainApiDataSource instance = null;
    private MainApiDataSource(){}
    public static MainApiDataSource getInstance() {
        if (instance == null) {
            synchronized (MainApiDataSource.class) {
                if (instance == null) {
                    instance = new MainApiDataSource();
                }
            }
        }
        return instance;
    }
    @Override
    public Observable<BaseResponse<AppToken>> getAppToken(@Query("app_id") String appId, @Query("private_key") String priveteKey) {
        return RetrofitHelper.createApi(MainApi.class,false,0).getAppToken(appId,priveteKey).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<UserToken>> isReLoginHypenate(@Query("user_token") String userToken) {
        return RetrofitHelper.createApi(MainApi.class,false,0).isReLoginHypenate(userToken).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<VerifyPhone>> verifyPhone(@Query("app_token") String appToken, @Query("member_type_key") String typeKey, @Query("mobile") String phone) {
        return RetrofitHelper.createApi(MainApi.class,true,0).verifyPhone(appToken, typeKey, phone).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<StringResponse> getPhoneCodeMsg(@Query("app_token") String appToken, @Query("service") String server) {
        return RetrofitHelper.createApi(MainApi.class,true,0).getPhoneCodeMsg(appToken, server).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<EmptyInfo>> getPhoneCode(@Query("app_token") String appToken, @Query("mobile") String phone, @Query("lock_value") String code) {
        return RetrofitHelper.createApi(MainApi.class,true,0).getPhoneCode(appToken,phone,code).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<PhoneCodeInfo>> getPhoneCodeNew(@Query("app_token") String appToken, @Query("mobile") String phone, @Query("lock_value") String code, @Query("image_code") String imageCode) {
        return RetrofitHelper.createApi(MainApi.class, true, 0).getPhoneCodeNew(appToken, phone, code, imageCode).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<UserToken>> login(@Query("app_token") String appToken, @Query("device_model") String deviceModel, @Query("mobile") String phone, @Query("data_source") String dataSource, @Query("request_info") String requestInfo, @Query("member_type_key") String key, @Query("sms_code") String code) {
        return RetrofitHelper.createApi(MainApi.class,true,0).login(appToken, deviceModel, phone, dataSource, requestInfo, key, code).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<UserInfo>> getUserInfo(@Query("user_token") String userToken) {
        return RetrofitHelper.createApi(MainApi.class,true,1).getUserInfo(userToken).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<ConsumerInfo>> getConsumerInfo(@Query("user_token") String userToken, @Query("service") String server, @Query("params") String params) {
        return RetrofitHelper.createApi(MainApi.class,true,0).getConsumerInfo(userToken, server, params).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<HyphenateInfo>> getHyphenateInfo(@Query("user_token") String userToken, @Query("service") String server, @Query("params") String params) {
        return RetrofitHelper.createApi(MainApi.class,true,1).getHyphenateInfo(userToken, server, params).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<UserAsset>> getUserAsset(@Query("user_token") String userToken, @Query("service") String server, @Query("params") String params) {
        return RetrofitHelper.createApi(MainApi.class,true,1).getUserAsset(userToken, server, params).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<CollectedStores>> getDefaultStores(@Query("app_token") String appToken, @Query("service") String server, @Query("params") String params) {
        return RetrofitHelper.createApi(MainApi.class,true,0).getDefaultStores(appToken, server, params).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<VersionInfo>> getVersionInfo(@Query("package_name") String packageName) {
        return RetrofitHelper.createApi(MainApi.class,true,0).getVersionInfo(packageName).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<UpdateBean>> getUpdateInfo(@Query("app_token") String appToken, @Query("service") String server, @Query("params") String params) {
        return RetrofitHelper.createApi(MainApi.class, true, 0).getUpdateInfo(appToken, server, params).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<RedPacketBean>> getRedPacket(@Query("user_token") String userToken, @Query("service") String server, @Query("params") String params) {
        return RetrofitHelper.createApi(MainApi.class,true,1).getRedPacket(userToken,server,params).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse> openRedPacket(@Query("user_token") String userToken, @Query("service") String server, @Query("params") String params) {
        return RetrofitHelper.createApi(MainApi.class,true,1).openRedPacket(userToken,server,params).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<BaseResponse<EmptyInfo>> postPushInfo(@Query("user_token") String userToken, @Query("member_type_key") String userType, @Query("device_type") String deviceType, @Query("device_model") String device, @Query("data_source") String source, @Query("alias") String alias) {
        return RetrofitHelper.createApi(MainApi.class, true, 1).postPushInfo(userToken, userType, deviceType, device, source, alias).compose(RxUtils.rxSchedulerHelper());
    }

    @Override
    public Observable<ResponseBody> downloadFile(@Url String fileUrl) {
        return RetrofitHelper.createApi(MainApi.class,false,0).downloadFile(fileUrl).compose(RxUtils.rxSchedulerHelper());
    }
}